| Conditions | 19 |
| Paths | 255 |
| Total Lines | 84 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like common.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright © 2017 Tangdongxin |
||
| 160 | function parse (str, re) { |
||
| 161 | |||
| 162 | str = reconvert(str); |
||
| 163 | let match; let val; let tmp; const i = 0; |
||
| 164 | const len = str.length; |
||
| 165 | try { |
||
| 166 | |||
| 167 | for (; match = re.exec(str);) { |
||
| 168 | |||
| 169 | val = match[0]; |
||
| 170 | if (val == '{' || val == '[') { |
||
| 171 | |||
| 172 | path.push(node); |
||
| 173 | node.appendChild(cache[val].cloneNode(true)); |
||
| 174 | node = node.lastChild.previousSibling; |
||
| 175 | node.len = 1; |
||
| 176 | node.start = re.lastIndex; |
||
| 177 | |||
| 178 | } else if ((val == '}' || val == ']') && node.len) { |
||
| 179 | |||
| 180 | if (node.childNodes.length) { |
||
| 181 | |||
| 182 | tmp = info.cloneNode(); |
||
| 183 | const content = node.len + ( |
||
| 184 | node.len == 1 ? |
||
| 185 | val == ']' ? ' item, ' : ' property, ' : |
||
| 186 | val == ']' ? ' items, ' : ' properties, ' |
||
| 187 | ) + units(re.lastIndex - node.start + 1); |
||
| 188 | |||
| 189 | if (hideDetails) { |
||
| 190 | |||
| 191 | tmp.setAttribute('title', content); |
||
| 192 | |||
| 193 | } else { |
||
| 194 | |||
| 195 | tmp.dataset.content = content; |
||
| 196 | |||
| 197 | } |
||
| 198 | |||
| 199 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 200 | |||
| 201 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, '\\\'')); |
||
| 202 | |||
| 203 | } |
||
| 204 | node.parentNode.insertBefore(tmp, node); |
||
| 205 | |||
| 206 | } else { |
||
| 207 | |||
| 208 | node.parentNode.removeChild(node); |
||
| 209 | |||
| 210 | } |
||
| 211 | node = path.pop(); |
||
| 212 | |||
| 213 | } else if (val == ',') { |
||
| 214 | |||
| 215 | node.len += 1; |
||
| 216 | node.appendChild(comma.cloneNode(true)); |
||
| 217 | |||
| 218 | } else { |
||
| 219 | |||
| 220 | tmp = span.cloneNode(); |
||
| 221 | tmp.textContent = match[1] || val; |
||
| 222 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 223 | node.appendChild(tmp); |
||
| 224 | if (match[3]) { |
||
| 225 | |||
| 226 | node.appendChild(colon.cloneNode()); |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 230 | } |
||
| 231 | |||
| 232 | } |
||
| 233 | document.title = ''; |
||
| 234 | JSON.parse(str); |
||
| 235 | |||
| 236 | } catch (e) { |
||
| 237 | |||
| 238 | dlog(e); |
||
| 239 | // TODO: find a better way to report error |
||
| 240 | |||
| 241 | } |
||
| 242 | |||
| 243 | } |
||
| 244 | |||
| 246 |